package day_05.protobuf;

import java.io.*;
import java.util.Arrays;

public class SerialTest {
    public static void main(String[] args) throws Exception {
        Teacher tim = new Teacher(1L, 34, "Tim");
        tim.getCourses().add("Java");

        // 序列化
        byte[] byteArray = serialize(tim);
        System.out.println(Arrays.toString(byteArray));

        // 反序列化
        Teacher teacher = deserialize(byteArray);
        System.out.println(teacher);
    }

    // 序列化
    private static byte[] serialize(Teacher tim) throws IOException {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(tim);
        return bos.toByteArray();
    }

    // 反序列化
    private static Teacher deserialize(byte[] bytes) throws Exception {
        ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes));
        return (Teacher)ois.readObject();
    }
}
